class Admin::PrintersController < Admin::AdminController
  before_action :get_printer,only: [:update,:edit,:destroy]
  before_action :get_restaurant
  
  def all_printers
    @printers = Printer.all
  end  

  def index
    @printers = Printer.where(restaurant_id: params[:restaurant_id])
  end

  def new
    @printer = Printer.new
  end

  def edit
  end  

  def create
    @printer = Printer.new(printers_params)
    @printer.restaurant_id = @restaurant.id
    if @printer.save
      redirect_to action: :index,restaurant_id: @printer.restaurant_id
    else
      render :new
    end  
  end

  def update
    if @printer.update_attributes(printers_params)
      redirect_to action: :index
    else
      render :edit
    end  
  end

  def destroy
    @printer.destroy
    redirect_to action: :index, restaurant_id: @printer.restaurant_id
  end


  private
  def get_restaurant
    @restaurant = Restaurant.find params[:restaurant_id]
  end  

  def get_restaurants
    @restaurants = Restuarant.all
  end  

  def get_printer
    @printer = Printer.find params[:id]
  end  

  def printers_params
    params.require(:printer).permit(:key,:name,:sn,:restaurant_id)
  end  
end
